package com.mystudy.leetcode.problem.link.p_24;

import com.mystudy.leetcode.base.ListNode;
import com.mystudy.leetcode.base.ListNodeUtils;
import org.junit.Test;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/6 14:56
 * @Description 两两交换链表中的节点
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        ListNode prev = dummyNode;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            ListNode next = cur.next;
            prev.next = next;

            cur.next = next.next;

            next.next = cur;

            prev = prev.next.next;

            cur = prev.next;


        }
        return dummyNode.next;
    }

    @Test
    public void test(){
        ListNode listNode = ListNodeUtils.generateLinkList(new int[]{1, 3, 4, 5});
        ListNode listNode1 = swapPairs(listNode);
        ListNodeUtils.show(listNode1);


    }
}
